<template>
  <div class="todo-footer">
    <label>
      <!-- 
        两种做法：
          1. 绑定checked属性和change事件
          2. 计算属性
      -->
      <input type="checkbox" v-model="isCheckedAll" />
    </label>
    <span>
      <span>已完成 {{ todoStore.isDoneNum }}</span> / 全部
      {{ todoStore.totalNum }}
    </span>
    <button
      class="btn btn-danger"
      v-show="todoStore.isDoneNum"
      @click="batchDelTodo"
    >
      清除已完成任务
    </button>
  </div>
</template>

<script lang="ts">
export default {
  name: "Footer",
};
</script>

<script lang="ts" setup>
import { computed } from "vue";
import { useTodoStore } from "../../store/modules/todo";

const todoStore = useTodoStore();

// 全选&全不选
const isCheckedAll = computed({
  get() {
    // !!todoStore.totalNum 防止值为0情况，需要转boolean值
    // 否则set方法中val类型就变成了number
    return todoStore.isDoneNum === todoStore.totalNum && !!todoStore.totalNum;
  },
  set(val) {
    todoStore.updateAllTodo(val);
  },
});

// 批量删除
const batchDelTodo = () => {
  todoStore.batchDelTodo();
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
